package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImp implements CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override//保存评论
    public Integer saveComment(Comment comment) {
        //调用mongo保存评论
        mongoTemplate.save(comment);
        //定义结果数量
        Integer resultCount = 0;
        //如果评论类中类型为点赞
        if (comment.getCommentType() == 1) {
            //根据动态id获取动态详情
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            //给动态详情中的点赞数+1
            movement.setLikeCount(movement.getLikeCount() + 1);
            //保存
            mongoTemplate.save(movement);
            //把保存后的点赞数付给结果数量
            resultCount = movement.getLikeCount();
            //如果评论类中类型为评论
        } else if (comment.getCommentType() == 2) {
            //根据动态id获取动态详情
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            //给动态详情中的评论数+1
            movement.setCommentCount(movement.getCommentCount() + 1);
            //保存
            mongoTemplate.save(movement);
            //把保存后的评论数付给结果数量
            resultCount = movement.getCommentCount();
            //如果评论类中类型为喜欢
        } else if (comment.getCommentType() == 3) {
            //根据动态id获取动态详情
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            //给动态详情中的喜欢数+1
            movement.setLoveCount(movement.getLoveCount() + 1);
            //保存
            mongoTemplate.save(movement);
            //把保存后的喜欢数付给结果数量
            resultCount = movement.getLoveCount();
        } else if (comment.getCommentType() == 4) {
            Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
            video.setLikeCount(video.getLikeCount() + 1);
            mongoTemplate.save(video);
            resultCount = video.getLikeCount();
        } else if (comment.getCommentType() == 5) {
            Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
            video.setCommentCount(video.getCommentCount() + 1);
            mongoTemplate.save(video);
            resultCount = video.getCommentCount();
        } else {
            resultCount = comment.getLikeCount();
        }
        return resultCount;
    }

    @Override//删除评论
    public Integer removeComment(Long userId, Integer commentType, String publishId) {
        //创建条件为userId、publishId和commentType同时相等的条件
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(commentType));
        //调用mongo删除满足条件的评论
        mongoTemplate.remove(query, Comment.class);
        //定义结果数量
        Integer resultCount = 0;
        //如果评论类中类型为点赞
        if (commentType == 1) {
            //根据动态id获取动态详情
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            //给动态详情中的喜欢数-1
            movement.setLikeCount(movement.getLikeCount() - 1);
            //保存
            mongoTemplate.save(movement);
            //把保存后的点赞数付给结果数量
            resultCount = movement.getLikeCount();
            //如果评论类中类型为评论
        } else if (commentType == 2) {
            //根据动态id获取动态详情
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            //给动态详情中的评价数-1
            movement.setCommentCount(movement.getCommentCount() - 1);
            //保存
            mongoTemplate.save(movement);
            //把保存后的评价数付给结果数量
            resultCount = movement.getCommentCount();
            //如果评论类中类型为喜欢
        } else if (commentType == 3) {
            //根据动态id获取动态详情
            Movement movement = mongoTemplate.findById(new ObjectId(publishId), Movement.class);
            //给动态详情中的喜欢数-1
            movement.setLoveCount(movement.getLoveCount() - 1);
            //保存
            mongoTemplate.save(movement);
            //把保存后的喜欢数付给结果数量
            resultCount = movement.getLoveCount();
        } else if (commentType == 4) {
            Video video = mongoTemplate.findById(new ObjectId(publishId), Video.class);
            video.setLikeCount(video.getLikeCount() - 1);
            mongoTemplate.save(video);
            resultCount = video.getLikeCount();
        }
        return resultCount;
    }

    @Override//查询评价分页
    public PageBeanVo findCommentByPage(String publishId, Integer pageNum, Integer pageSize, Integer commentType) {
        //计算分页开始索引
        Integer index = (pageNum - 1) * pageSize;
        //创建条件为动态id和类型为评价 并按照创建时间降序 分页
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        //调用mongo查询满足条件的评价集合
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        //查询满足条件的评价数量
        long count = mongoTemplate.count(query, Comment.class);
        //返回分页类
        return new PageBeanVo(pageNum, pageSize, count, comments);
    }

    @Override //查询点赞、评论、喜欢列表并分页
    public PageBeanVo findUserCommentByPage(Long publishUserId, Integer pageNum, Integer pageSize, Integer commentType) {
        //创建条件为发布人id和评论类型相同的 并按照创建时间降序 分页
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId)
                .and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        //根据条件查询集合
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        //查询满足条件的数量
        long count = mongoTemplate.count(query, Comment.class);
        //封装到分页类
        return new PageBeanVo(pageNum, pageSize, count, comments);
    }

    @Override//根据id查询评论
    public Comment findCommentById(ObjectId id) {
        return mongoTemplate.findById(id, Comment.class);
    }


    @Override//添加视频评论点赞
    public void addVideoCommentsLike(String contentId, Comment addComment) {
        //调用mongo根据主键查询出评论详情
        Comment comment = mongoTemplate.findById(new ObjectId(contentId), Comment.class);
        //给评论点赞属性+1
        comment.setLikeCount(comment.getLikeCount() + 1);
        //调用mongo保存
        mongoTemplate.save(comment);
        mongoTemplate.save(addComment);
    }

    @Override//取消视频评论点赞
    public void delVideoCommentsLike(String contentId, Long userId, Integer commentType, Long publishUserId) {
        //调用mongo根据主键查询评论详情
        Comment comment = mongoTemplate.findById(new ObjectId(contentId), Comment.class);
        //给评论点赞数据-1
        comment.setLikeCount(comment.getLikeCount() - 1);
        //调用mongo保存
        mongoTemplate.save(comment);
        //删除条件为用户id 发布人id 并且类型为视频评论点赞的数据
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("publishUserId").is(publishUserId)
                .and("commentType").is(commentType));
        mongoTemplate.remove(query, Comment.class);
    }

    @Override
    public Integer addMovementCommentsLike(ObjectId publishId, Comment comment) {
        Comment comment1 = mongoTemplate.findById(publishId, Comment.class);
        comment1.setLikeCount(comment1.getLikeCount()+1);
        mongoTemplate.save(comment);
        mongoTemplate.save(comment1);
        return comment1.getLikeCount();
    }

    @Override
    public Integer delMovementCommentsLike(ObjectId publishId,Integer commentType) {
        Comment comment = mongoTemplate.findById(publishId, Comment.class);
        comment.setLikeCount(comment.getLikeCount()-1);
        mongoTemplate.save(comment);
        Query query=new Query(Criteria.where("publishId").is(publishId)
        .and("commentType").is(commentType));
        mongoTemplate.remove(query,Comment.class);
        return comment.getLikeCount();
    }
}
